package com.wenxin.administrator.zhufengfm;

import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.widget.RadioGroup;
import fragments.CustomerFragment;
import fragments.DiscoverFragment;
import fragments.DownloadTingFragment;
import fragments.PersonalFragment;

public class MainActivity extends FragmentActivity implements RadioGroup.OnCheckedChangeListener {

    private Fragment fragment[];
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_activity);
        //创建一个Fragment的集合
        fragment=new Fragment[4];
        fragment[0]=new DiscoverFragment();
        fragment[1]=new CustomerFragment();
        fragment[2]=new DownloadTingFragment();
        fragment[3]=new PersonalFragment();
        //用来管理Fragment的管理类
      FragmentManager  manager=getSupportFragmentManager();
        if(savedInstanceState!=null)
        {
            int len=fragment.length;
            for(int i=0;i<len;i++)
            {
                //设置标签
                fragment[i]=manager.findFragmentByTag("f"+i);
            }
        }
        //开启一个事务Fragment的
        FragmentTransaction transaction=manager.beginTransaction();
        int len=fragment.length;
        for(int i=0;i<len;i++)
        {
            //将集合中的Fragment添加到FragmentUI中
            transaction.add(R.id.main_fragment_container,fragment[i]);
            //隐藏所有的Fragment
            transaction.hide(fragment[i]);
        }
        //显示第一个Fragment
        transaction.show(fragment[0]);
        //提交事务
        transaction.commit();
        //获取radioGroup
        RadioGroup radioGroup= (RadioGroup) findViewById(R.id.main_tan_bar);
        //注册事件
        radioGroup.setOnCheckedChangeListener(this);

    }

    public void onCheckedChanged(RadioGroup radioGroup, int i) {
        int index=0;
         switch (i){
             case R.id.main_tab_discover:
                 index=0;
                 break;
             case R.id.main_tab_custom:
                 index=1;
                 break;
             case R.id.main_tab_download:
                 index=2;
                 break;
             case R.id.main_tab_personal:
                 index=3;
                 break;

         }
        int len=fragment.length;
        FragmentManager manager=getSupportFragmentManager();
        FragmentTransaction transaction=manager.beginTransaction();
        for (int j = 0; j <len ; j++) {
            if(j==index){
                //显示当前点击的Fragment
                transaction.show(fragment[j]);
            }else{
                transaction.hide(fragment[j]);
            }
        }
        //提交事务
        transaction.commit();
    }
}
